﻿using System;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;

namespace Trippy.Abilities
{
    public abstract class Ability //parent class for future abilities
    {
        protected Ability(Level level)
        {
            CurrentLevel = level;
            if (font == null)
                font = CurrentLevel.Game.Content.Load<SpriteFont>("Fonts/timer");
            State = AbilityState.Available;
        }

        public enum AbilityState { Available, Active, Unavailable, Passive }

        public String name { get; protected set; }
        public Texture2D icon { get; protected set; }
        public AbilityState State;
        static protected SpriteFont font;
        public Color color
        {
            get
            {
                switch (State)
                {
                    case AbilityState.Available:
                        return Color.White;
                    case AbilityState.Active:
                        return Color.Blue;
                    case AbilityState.Unavailable:
                        return Color.Red;
                    case AbilityState.Passive:
                        return Color.Green;
                    default:
                        return Color.White;
                }
            }
        }

        public Level CurrentLevel { get; protected set; }

        protected float timer;
        protected bool timerActive;

        protected virtual void SetTimer(float time) //set the timer to some time. can only handle one at a time right now. lemme know if you need more.
        {
            timer = time;
            timerActive = true;
        }
        protected virtual void KillTimer() //kill timer without triggering onTimer() event.
        {
            timerActive = false;
        }
        protected virtual void OnTimer() //event called when timer hits 0.
        {
            timerActive = false;
        }

        public virtual void OnMoveLeft(float elapsedTime)
        {
        }

        public virtual void OnMoveRight(float elapsedTime)
        {
        }

        public virtual void OnActivate() //called when ability button is pressed, only called if State = Available;
        {
        }
        public virtual void OnCancel() //called when ability button is pressed and State = Active;
        {
        }
        public virtual void OnHold(float elapsedTime) //called when ability button is down;
        {
        }
        public virtual void OnRelease() //called when ability button released;
        {
        }
        public virtual void OnLand() //event when hero lands on ground, note that currently this while be called repeatedly when hero is on ground
        {
        }
        public virtual void OnJump() //event when hero *PRESSES JUMP BUTTON* not necessarily jumps
        {
            OnAir();
        }
        public virtual void OnAir() //event when hero moves to air for any reason
        {
        }
        public virtual bool OnCollide(CollidableGameComponent component, Trippy.CollidableGameComponents.CollidableGameComponent.CollisionSide side) 
            //event when hero collides with something else
            //return true to override normal collision detection.
        {
            return false;
        }
        public virtual void OnRemove() //event when ability is removed from hero, do NOT change hero's ability in here.
        {
        }
        public virtual void OnAdd() // event when ability is added to hero, do NOT change hero's ability here eihter.
        {
            State = AbilityState.Available; //can't use an ability just after getting it. design choice here.
            KillTimer(); //stops restored abilities from having active timers.
        }
        public virtual bool OnDie(Hero.MannerOfDeath d) //called when hero is about to die, return false to prevent the death.
        {
            return true;
        }
        public virtual void OnDraw(SpriteBatch sb, Rectangle r) //icon drawing
        {
            sb.Draw(icon, r, color);
            if(timerActive)
                sb.DrawString(font, timer.ToString("f1"), new Vector2(r.X - 5, r.Y + r.Height - 10), CurrentLevel.UIColor);
        }

        public virtual void OnUpdate(float elapsedTime) //updated concurrently with hero
        {
            if (timerActive)
            {
                timer -= elapsedTime;
                if (timer <= 0f)
                {
                    OnTimer();
                }
            }
        }
    }
}
